using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VBASTKE
    {
        public DataRow Convert2BASTKE(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryBASTKE(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["BAKMANDANT"] = " ".PadRight(2 ); 
         dwReturn["BAKART"] = " ".PadRight(20 ); 
         dwReturn["BAKVAR"] = " ".PadRight(2 ); 
         dwReturn["BAKFARBE"] = " ".PadRight(10 ); 
         dwReturn["BAKPOS"] = " ".PadRight(2 ); 
         dwReturn["BAKAENZL"] = " ".PadRight(0 ); 
         dwReturn["BAKAENB"] = " ".PadRight(8 ); 
         dwReturn["BAKAEND"] = " ".PadRight(0 ); 
         dwReturn["BAKNEUB"] = " ".PadRight(8 ); 
         dwReturn["BAKNEUD"] = " ".PadRight(0 ); 
         dwReturn["BAKKART"] = " ".PadRight(20 ); 
         dwReturn["BAKKVAR"] = " ".PadRight(2 ); 
         dwReturn["BAKKFARBE"] = " ".PadRight(10 ); 
         dwReturn["BAKPATT"] = " ".PadRight(6 ); 
         dwReturn["BAKANZ"] = " ".PadRight(0 ); 
         dwReturn["BAKVARKZ"] = " ".PadRight(1 ); 
         dwReturn["BAKABF"] = " ".PadRight(0 ); 
         dwReturn["BAKDISPOKZ"] = " ".PadRight(1 ); 
         dwReturn["BAKFILLER"] = " ".PadRight(44 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapBASTKE(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveBASTKE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryBASTKE(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapBASTKE(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertBASTKE(dwLocal, ITXDBVERSION);
            }
            UpdateBASTKE(dwLocal);
        }
        public void InsertBASTKE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO BASTKE(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryBASTKE(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM BASTKE WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM BASTKE  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "BASTKE";
            Return = Result;
            return Return;
        }
        public void DeleteBASTKE(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM BASTKE   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateBASTKE(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE BASTKE SET "
                    +"BAKMANDANT= :BAKMANDANT,"
                    +"BAKART= :BAKART,"
                    +"BAKVAR= :BAKVAR,"
                    +"BAKFARBE= :BAKFARBE,"
                    +"BAKPOS= :BAKPOS,"
                    +"BAKAENZL= :BAKAENZL,"
                    +"BAKAENB= :BAKAENB,"
                    +"BAKAEND= :BAKAEND,"
                    +"BAKNEUB= :BAKNEUB,"
                    +"BAKNEUD= :BAKNEUD,"
                    +"BAKKART= :BAKKART,"
                    +"BAKKVAR= :BAKKVAR,"
                    +"BAKKFARBE= :BAKKFARBE,"
                    +"BAKPATT= :BAKPATT,"
                    +"BAKANZ= :BAKANZ,"
                    +"BAKVARKZ= :BAKVARKZ,"
                    +"BAKABF= :BAKABF,"
                    +"BAKDISPOKZ= :BAKDISPOKZ,"
                    +"BAKFILLER= :BAKFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("BAKMANDANT", OracleType.NVarChar).Value = dwData["BAKMANDANT"].ToString(); 
          or1.Parameters.Add("BAKART", OracleType.NVarChar).Value = dwData["BAKART"].ToString(); 
          or1.Parameters.Add("BAKVAR", OracleType.NVarChar).Value = dwData["BAKVAR"].ToString(); 
          or1.Parameters.Add("BAKFARBE", OracleType.NVarChar).Value = dwData["BAKFARBE"].ToString(); 
          or1.Parameters.Add("BAKPOS", OracleType.NVarChar).Value = dwData["BAKPOS"].ToString(); 
          or1.Parameters.Add("BAKAENZL", OracleType.NVarChar).Value = dwData["BAKAENZL"].ToString(); 
          or1.Parameters.Add("BAKAENB", OracleType.NVarChar).Value = dwData["BAKAENB"].ToString(); 
          or1.Parameters.Add("BAKAEND", OracleType.NVarChar).Value = dwData["BAKAEND"].ToString(); 
          or1.Parameters.Add("BAKNEUB", OracleType.NVarChar).Value = dwData["BAKNEUB"].ToString(); 
          or1.Parameters.Add("BAKNEUD", OracleType.NVarChar).Value = dwData["BAKNEUD"].ToString(); 
          or1.Parameters.Add("BAKKART", OracleType.NVarChar).Value = dwData["BAKKART"].ToString(); 
          or1.Parameters.Add("BAKKVAR", OracleType.NVarChar).Value = dwData["BAKKVAR"].ToString(); 
          or1.Parameters.Add("BAKKFARBE", OracleType.NVarChar).Value = dwData["BAKKFARBE"].ToString(); 
          or1.Parameters.Add("BAKPATT", OracleType.NVarChar).Value = dwData["BAKPATT"].ToString(); 
          or1.Parameters.Add("BAKANZ", OracleType.NVarChar).Value = dwData["BAKANZ"].ToString(); 
          or1.Parameters.Add("BAKVARKZ", OracleType.NVarChar).Value = dwData["BAKVARKZ"].ToString(); 
          or1.Parameters.Add("BAKABF", OracleType.NVarChar).Value = dwData["BAKABF"].ToString(); 
          or1.Parameters.Add("BAKDISPOKZ", OracleType.NVarChar).Value = dwData["BAKDISPOKZ"].ToString(); 
          or1.Parameters.Add("BAKFILLER", OracleType.NVarChar).Value = dwData["BAKFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
